import json
from pyoauth2 import Client
import requests


KEY = '2089783351'
SECRET = 'dcf4f52213b5a55d856a078ccf0130d7'
CALLBACK = 'http://xgz.xkk.pw/social/bind/weibo'
USERINFO_URL = 'https://api.weibo.com/2/users/show.json'


client = Client(KEY, SECRET,
                site='https://api.weibo.com',
                authorize_url='https://api.weibo.com/oauth2/authorize',
                token_url='https://api.weibo.com/oauth2/access_token',
)


authorize_url = client.auth_code.authorize_url(redirect_uri=CALLBACK)


def get_token_response(code_str):
    code_str = code_str.strip()
    payload = {'client_id': KEY, 'client_secret': SECRET,
               'redirect_uri': CALLBACK, 'grant_type': 'authorization_code',
               'code': code_str}
    response = requests.post(client.token_url(), params=payload)
    response = json.loads(response.text)
    return response


def get_user_info(token, uid):
    payload = {'access_token': token, 'uid': uid}
    response = requests.get(USERINFO_URL, params=payload)
    return response.text
